using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Controls;
using WP.Modules;
using WP.MF.DataAccess;

namespace WP.MF.Form
{
    /// <summary>
    /// called by Security Groups Maintenance button view
    /// 20070724 YJ
    /// </summary>
    public partial class frmMUserMenuView : BaseQryRpt
    {

        #region Define Variable

        private DBAccess mdbAccess = null;

        private string mstrUserName = string.Empty;

        private string mstrModuleName = string.Empty;

        #endregion

        #region Constructor Functions

        public frmMUserMenuView()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            mdbAccess = new DBAccess(false, false);
        }

        public void ShowForm(string strUserName, string strModuleName)
        {
            this.mstrUserName = strUserName;
            this.mstrModuleName = strModuleName;
            if (BaseFunctions.ExistModalWindow())
            {
                this.ShowDialog();
            }
            else
            {
                this.MdiParent = SysVar.MDIParentForm;
                this.Show();
            }
        }
        #endregion

        #region Private Events

        private void frmMUserMenuView_Load(object sender, EventArgs e)
        {
            mstrUserName = SysVar.UserName;

            txtUserName.Text = mstrUserName;

            if (mstrUserName.Trim() != string.Empty && mstrModuleName.Trim() != string.Empty)
                Create_TreeView(tvUserMenu);
            else
                return;
        }
        private void frmMUserMenuView_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdbAccess.CloseCon();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strSql = string.Empty;
            ButtonEx ubt = sender as ButtonEx;
            bool blnHasSetFocus = false;
            Control con = null;

            string strCaption = string.Empty;
            string strFindValue = string.Empty;
            string strLookUpField = string.Empty;

            switch (ubt.Name)
            {
                case "ubtUserName":
                    strSql = "select username,description from muser order by username";
                    strCaption = "User";
                    strFindValue = this.txtUserName.Text;
                    strLookUpField = "username";
                    con = txtUserName;
                    break;
                default:
                    base.ubtLookUp_Click(sender, e);
                    return;
                    break;
            }

            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            WP.Modules.frmLookUp frmlookup = new WP.Modules.frmLookUp(mdbAccess, strSql, strCaption, strFindValue, strLookUpField);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                if (con != null)
                {
                    con.Text = frmlookup.GetReturnValue(strLookUpField);
                    if (!blnHasSetFocus)
                    {
                        con.Focus();
                        blnHasSetFocus = true;
                    }
                }
            }
            frmlookup.Close();
        }
        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            if ( mstrModuleName.Trim() != string.Empty  && !mstrUserName.Equals( txtUserName.Text) )
            {
                mstrUserName = txtUserName.Text;
                Create_TreeView(tvUserMenu);
            }
            else
                return;
        }

        #endregion

        #region Private Functions

        private void Create_TreeView(TreeView tvObject)
        {
            // Function: Create tree in tvStyle or in tvEdit
            int intTotal = 0;
            TreeNode nodeSel = null;
            TreeNode nodX = null;
            string strMenuSql = string.Empty;
            string strUserMenuSql = string.Empty;
            string strGroup = string.Empty;
            string strKey = string.Empty;
            string strSql = string.Empty;
            DBAccess dBAccess = new DBAccess(false, false);
            DataRow drGroup = null;
            DataTable dtMenu = null;
            DataTable dtUserMenu = null;
            DataView dvMenu = null;

            tvObject.ImageList = this.iglMenu;
            if (tvObject.Nodes.Count > 0) tvObject.Nodes.Clear();

            strSql = "select glSecurGrp,apSecurGrp,arSecurGrp,oeSecurGrp,imSecurGrp,poSecurGrp,rmSecurGrp,twSecurGrp,";
            strSql += "tpSecurGrp,smSecurGrp,mfSecurGrp from mUser where UserName='" + mstrUserName + "'";
            drGroup = dBAccess.GetDataFirstRow(strSql);

            switch (mstrModuleName)  // because key like "Default??"
            {
                case "GL":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["glSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.GL + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "AP":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["apSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.AP + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "AR":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["arSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.AR + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "OE":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["oeSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.OE + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "IM":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["imSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.IM + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "PO":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["poSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.PO + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "RM":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["rmSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.RM + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "TW":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["twSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.TW + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "TP":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["tpSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.TP + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "ES":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["smSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.ES + "' and Permit" + strGroup + "<>'N')";
                    break;

                case "MF":
                    strGroup = string.Format("{0:00}", CvtFunc.ToInt(drGroup["mfSecurGrp"]));
                    strMenuSql += " or (MenuID like '" + ModuleInfo.StrModule.MF + "' and Permit" + strGroup + "<>'N')";
                    break;

                default:       // user defined menu          key like "UserMenu%"
                    strUserMenuSql += " or UserMenuNo=" + (nodeSel.Name.ToString().Substring(8));
                    break;
            }


            if (strMenuSql != string.Empty)
            {
                strMenuSql = " where " + strMenuSql.Substring(strMenuSql.Length - (strMenuSql.Length - 4));
                strMenuSql = "select MenuID,MenuName,IconName from mMenu" + strMenuSql + " and NotUsed=0 and progStatus = 'D' order by MenuID";
                dtMenu = dBAccess.GetDataTable(strMenuSql);
                intTotal = dtMenu.Rows.Count;
            }

            if (strUserMenuSql != string.Empty)
            {
                strUserMenuSql = " where (" + strUserMenuSql.Substring(strUserMenuSql.Length - (strUserMenuSql.Length - 4)) + " ) and UserName='" + SysVar.UserName + "'";
                strUserMenuSql = "select UserMenuNo,UserMenuID,MenuID,MenuName from mUserMenu" + strUserMenuSql + " order by UserMenuNo,UserMenuID";
                dtUserMenu = dBAccess.GetDataTable(strUserMenuSql);
                intTotal = dtUserMenu.Rows.Count + intTotal;
            }


            TreeNode[] tnArr = null;
            TreeNode tn = null;

            // create default menu tree            key start as "D"
            if (strMenuSql != string.Empty)
            {
                foreach (DataRow drMenu in dtMenu.Rows)
                {
                    if (drMenu["MenuId"].ToString().Substring(drMenu["MenuId"].ToString().Length - 9) == "000000000")
                    {
                        strKey = "D" + drMenu["MenuId"].ToString().Trim().Substring(0, 3);
                        nodX = tvObject.Nodes.Add(strKey, drMenu["MenuName"].ToString().Trim(), drMenu["IconName"].ToString().Trim(), drMenu["IconName"].ToString().Trim());
                        nodX.Tag = drMenu["MenuId"].ToString().TrimEnd();
                    }
                    else
                    {
                        strKey = "D" + drMenu["MenuId"].ToString().Trim();
                        if (strKey.Substring(strKey.Length - 6) == "000000") strKey = strKey.Substring(0, 7);
                        if (strKey.Substring(strKey.Length - 3) == "000") strKey = strKey.Substring(0, 10);
                        if (tvObject.Nodes.Find(strKey.Substring(0, strKey.Length - 3), true) != null) //???
                        {
                            tnArr = tvObject.Nodes.Find(strKey.Substring(0, strKey.Length - 3), true);
                            if (tnArr.Length != 0)
                            {
                                tn = tnArr[0];
                                nodX = tn.Nodes.Add(strKey, drMenu["MenuName"].ToString().Trim(), drMenu["IconName"].ToString().Trim(), drMenu["IconName"].ToString().Trim());
                                nodX.Tag = drMenu["MenuId"].ToString().TrimEnd();
                            }
                        }
                    }
                }
            }

            // create user menu tree               key start as "N*****U"   N***** means user menu no
            if (strUserMenuSql != string.Empty)
            {
                strMenuSql = "select MenuID,IconName,NotUsed from mMenu where MenuID in " +
                        "(select MenuID from mUserMenu where UserName='" + mstrUserName + "')";
                dvMenu = dBAccess.GetDataTable(strMenuSql).DefaultView;

                foreach (DataRow drUserMenu in dtUserMenu.Rows)
                {
                    dvMenu.RowFilter = "MenuID='" + drUserMenu["MenuID"].ToString() + "'";
                    if (dvMenu.Count > 0)
                    {
                        if (!CvtFunc.ToBoolean(dvMenu[0]["NotUsed"]))
                        {
                            if (drUserMenu["UserMenuID"].ToString().Substring(drUserMenu["UserMenuID"].ToString().Length - 9) == "000000000")
                            {
                                strKey = "N" + string.Format("{0:00000}", CvtFunc.ToInt(drUserMenu["UserMenuNo"])) + "U" + drUserMenu["UserMenuID"].ToString().Trim().Substring(0, 3);
                                nodX = tvObject.Nodes.Add(strKey, drUserMenu["MenuName"].ToString().Trim(), dvMenu[0]["IconName"].ToString().Trim(), dvMenu[0]["IconName"].ToString().Trim());
                                nodX.Tag = drUserMenu["MenuId"].ToString().TrimEnd();
                            }
                            else
                            {
                                strKey = "N" + string.Format("{0:00000}", CvtFunc.ToInt(drUserMenu["UserMenuNo"])) + "U" + drUserMenu["UserMenuID"].ToString().Trim();
                                if (strKey.Substring(strKey.Length - 6) == "000000") strKey = strKey.Substring(0, 13);
                                if (strKey.Substring(strKey.Length - 3) == "000") strKey = strKey.Substring(0, 16);
                                if (tvObject.Nodes.Find(strKey.Substring(0, strKey.Length - 3), true) != null)
                                {
                                    tnArr = tvObject.Nodes.Find(strKey.Substring(0, strKey.Length - 3), true);
                                    if (tnArr.Length != 0)
                                    {
                                        tn = tnArr[0];
                                        nodX = tn.Nodes.Add(strKey, drUserMenu["MenuName"].ToString().Trim(), dvMenu[0]["IconName"].ToString().Trim(), dvMenu[0]["IconName"].ToString().Trim());
                                        nodX.Tag = drUserMenu["MenuId"].ToString().TrimEnd();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (tvObject.Nodes.Count > 0) tvObject.Nodes[0].Expand();  
        }

        #endregion

    }
}